/**
 * 2018年5月22日上午12:06:50
 */
package com.an.nopro.app.framework.schedulejob;

import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.Job;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 
 * @目的：.<br>
 * 
 * @author 86171
 * @date 2018年5月22日上午12:06:50
 * 
 */
@RestController
public class AddJobController {

	@Autowired
	private SchedulerFactoryBean schedulerFactoryBean;

	@GetMapping("addjob")
	public String addJob() throws ClassNotFoundException, SchedulerException {
		@SuppressWarnings("unchecked")
		JobDetail detail = JobBuilder
				.newJob((Class<? extends Job>) Class.forName("com.an.nopro.app.framework.schedulejob.QuartzJobHello"))
				.withIdentity("aa", "gg").build();
		CronTriggerFactoryBean tigger = new CronTriggerFactoryBean();
		tigger.setCronExpression("0/3 * * * * ?");// 初始时的cron表达式
		tigger.setName("srd-chhliu");// trigger的name
		tigger.setGroup("ddd");
		tigger.setJobDetail(detail);
		Scheduler scheduler = schedulerFactoryBean.getObject();

		// trigger 获取对应运行类的名称，设置成其Trigger名称及Trigger组
		Trigger trigger = TriggerBuilder.newTrigger()
				.withIdentity(this.getClass().getSimpleName() + "_Trigger", this.getClass().getSimpleName() + "_group")
				.startNow().withSchedule(CronScheduleBuilder.cronSchedule("0/4 * * * * ?")).build();
		scheduler.scheduleJob(detail,trigger);

		return "添加定时任务";
	}
}
